package com.jinzhi.jzweb.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.dao.JazCoursesOrderDao;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.service.UserService;
import com.jinzhi.wxpay.enums.OrderStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazUserCommentDao;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2023-04-18 09:46:08 | Long</small>
 */
@Service
public class JazUserCommentServiceImpl extends CoreServiceImpl<JazUserCommentDao, JazUserCommentDO> implements JazUserCommentService {

    @Autowired
    private JazCoursesService jazCoursesService;

    @Autowired
    private JazCoursesOrderDao jazCoursesOrderDao;

    @Autowired
    private JazGroupOrderService jazGroupOrderService;

    @Autowired
    private JazUserOrderService jazUserOrderService;

    @Autowired
    private UserService userService;

    @Autowired
    private JazUserCommentClassifyService jazUserCommentClassifyService;

    @Autowired
    private JazUserCommentDescriptionService jazUserCommentDescriptionService;

    @Autowired
    private JazSysProjectService jazSysProjectService;

    @Autowired
    private JazGroupProdService jazGroupProdService;



    /**
     * 分页查询评价列表
     *
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public Page<JazUserCommentDO> getUserCommentList(Integer type,Long commentdObjectid,Integer current, Integer pageSize) {
        // 查询列表数据
        Wrapper<JazUserCommentDO> wrapper = new EntityWrapper<JazUserCommentDO>().orderBy("comment_time", false);
        if(null!=type){
            wrapper.eq("type",type);
        }
        if(null!=commentdObjectid){
            wrapper.eq("commentd_objectid",commentdObjectid);
        }
        Page<JazUserCommentDO> page1 = new Page<JazUserCommentDO>(current,pageSize);
        Page<JazUserCommentDO> page = selectPage(page1,wrapper);
        List<JazUserCommentDO> list = new ArrayList();
        // 封装数据
        List<JazUserCommentDO> records = page.getRecords();
        //for (JazUserCommentDO jazUserCommentDO : records) {
        records.stream().forEach(jazUserCommentDO -> {
            JazUserCommentDO userCommentDO = new JazUserCommentDO();
            BeanUtils.copyProperties(jazUserCommentDO,userCommentDO);
            // 课程评价
            if (jazUserCommentDO.getType()==1 && jazUserCommentDO.getCommentOrderId()!=null){
                // 获取课程订单信息
                JazCoursesOrderDO jazCoursesOrderDO = jazCoursesOrderDao.selectById(jazUserCommentDO.getCommentOrderId());
                coursesComment(jazUserCommentDO, jazCoursesOrderDO, userCommentDO, list);
            }
            // 服务评价
            else if (jazUserCommentDO.getType()==2 && jazUserCommentDO.getCommentOrderId()!=null){
                // 获取服务订单信息
                JazUserOrderDO jazUserOrderDO = jazUserOrderService.selectById(jazUserCommentDO.getCommentOrderId());
                userOrderComment(jazUserCommentDO, jazUserOrderDO, userCommentDO, list);
            }
            // 团购评价
            else if (jazUserCommentDO.getType()==3 && jazUserCommentDO.getCommentOrderId()!=null){
                // 获取团购订单信息
                JazGroupOrderDO jazGroupOrderDO = jazGroupOrderService.selectById(jazUserCommentDO.getCommentOrderId());
                groupComment(jazUserCommentDO, jazGroupOrderDO, userCommentDO, list);
            }
        //}
        });
        page.setRecords(list);
        return page;
    }

    /**
     * 新增评论
     * @param jazUserCommentDTO
     * @return
     */
    @Override
    public Result<?>  addComment(JazUserCommentDO jazUserCommentDTO) {
        Assert.notNull(jazUserCommentDTO.getCommentOrderId(),"commentOrderId"+"不能为空");
        Assert.notNull(jazUserCommentDTO.getCommentUserid(),"commentUserid"+"不能为空");
        Assert.notNull(jazUserCommentDTO.getCommentdObjectid(),"commentdObjectid"+"不能为空");
        JazCoursesOrderDO jazCoursesOrderDO1 = new JazCoursesOrderDO();
        jazCoursesOrderDO1.setUserId(jazUserCommentDTO.getCommentUserid());
        jazCoursesOrderDO1.setCoursesId(jazUserCommentDTO.getCommentdObjectid());
        jazCoursesOrderDO1.setStatus(OrderStatus.SUCCESS.getType());
        JazCoursesOrderDO jazCoursesOrderDO = jazCoursesOrderDao.selectOne(jazCoursesOrderDO1);
        if(jazCoursesOrderDO==null || jazCoursesOrderDO.getId().longValue() != jazUserCommentDTO.getCommentOrderId().longValue()){
            return Result.fail("请先购买再评价");
        }

        jazUserCommentDTO.setCommentTime(new Date());
        jazUserCommentDTO.setType(1);
        this.insert(jazUserCommentDTO);

        //修改订单的评论状态为：已评论
        JazCoursesOrderDO jazCoursesOrderDO2 = new JazCoursesOrderDO();
        jazCoursesOrderDO2.setHasComment(1);
        jazCoursesOrderDO2.setId(jazUserCommentDTO.getCommentOrderId());
        jazCoursesOrderDao.updateById(jazCoursesOrderDO2);

        return Result.ok(jazUserCommentDTO);
    }

    /**
     * 团购评价
     * @param jazUserCommentDO
     * @param jazGroupOrderDO
     * @param userCommentDO
     * @param list
     */
    private void groupComment(JazUserCommentDO jazUserCommentDO, JazGroupOrderDO jazGroupOrderDO, JazUserCommentDO userCommentDO, List<JazUserCommentDO> list) {
        if (jazGroupOrderDO !=null){
            UserDO userDO = userService.selectById(jazGroupOrderDO.getUserId());
            if (StringUtil.notEmpty(userDO.getName())){
                userCommentDO.setName(userDO.getName());
            }else {
                userCommentDO.setName(userDO.getNickname());
            }
            // 获取评论对象
            JazGroupProdDO jazGroupProdDO = jazGroupProdService.selectById(jazGroupOrderDO.getGroupProdId());
            userCommentDO.setCommentObject(jazGroupProdDO.getName());
        }
        // 评价描述
        String[] split = jazUserCommentDO.getCommentDescriptionId().split(",");
        Wrapper<JazUserCommentDescriptionDO> wrapper = new EntityWrapper<JazUserCommentDescriptionDO>().in("id", split);
        List<JazUserCommentDescriptionDO> userCommentDescriptionDOList = jazUserCommentDescriptionService.selectList(wrapper);
        List<String> stringArrayList = new ArrayList<>();
        Long commentClassifyId=userCommentDescriptionDOList.get(0).getCommentClassifyId();
        userCommentDescriptionDOList.stream().forEach(a->{
            stringArrayList.add(a.getCommentDescription());
        });
        userCommentDO.setCommentDescriptionContext(stringArrayList);
        // 评价类型
        JazUserCommentClassifyDO jazUserCommentClassifyDO = jazUserCommentClassifyService.selectById(commentClassifyId);
        userCommentDO.setCommentTypeName(jazUserCommentClassifyDO.getCommentTypeName());
        list.add(userCommentDO);
    }

    /**
     * 服务评价
     * @param jazUserCommentDO
     * @param jazUserOrderDO
     * @param userCommentDO
     * @param list
     */
    private void userOrderComment(JazUserCommentDO jazUserCommentDO, JazUserOrderDO jazUserOrderDO, JazUserCommentDO userCommentDO, List<JazUserCommentDO> list) {
        if (jazUserOrderDO !=null){
            UserDO userDO = userService.selectById(jazUserOrderDO.getInitiateId());
            if (StringUtil.notEmpty(userDO.getName())){
                userCommentDO.setName(userDO.getName());
            }else {
                userCommentDO.setName(userDO.getNickname());
            }
            // 获取评论对象
            if (StringUtil.notEmpty(jazUserOrderDO.getProjectId())){
                String[] projects = jazUserOrderDO.getProjectId().split(",");
                List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
                userCommentDO.setCommentObject(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
            }
        }
        // 评价描述
        String[] split = jazUserCommentDO.getCommentDescriptionId().split(",");
        Wrapper<JazUserCommentDescriptionDO> wrapper = new EntityWrapper<JazUserCommentDescriptionDO>().in("id", split);
        List<JazUserCommentDescriptionDO> userCommentDescriptionDOList = jazUserCommentDescriptionService.selectList(wrapper);
        List<String> stringArrayList = new ArrayList<>();
        Long commentClassifyId=userCommentDescriptionDOList.get(0).getCommentClassifyId();
        userCommentDescriptionDOList.stream().forEach(a->{
            stringArrayList.add(a.getCommentDescription());
        });
        userCommentDO.setCommentDescriptionContext(stringArrayList);
        // 评价类型
        JazUserCommentClassifyDO jazUserCommentClassifyDO = jazUserCommentClassifyService.selectById(commentClassifyId);
        userCommentDO.setCommentTypeName(jazUserCommentClassifyDO.getCommentTypeName());
        list.add(userCommentDO);
    }

    /**
     * 课程评价
     * @param jazUserCommentDO
     * @param jazCoursesOrderDO
     * @param userCommentDO
     * @param list
     */
    private void coursesComment(JazUserCommentDO jazUserCommentDO, JazCoursesOrderDO jazCoursesOrderDO, JazUserCommentDO userCommentDO, List<JazUserCommentDO> list) {
        if (jazCoursesOrderDO !=null){
            UserDO userDO = userService.selectById(jazCoursesOrderDO.getUserId());
            if (StringUtil.notEmpty(userDO.getName())){
                userCommentDO.setName(userDO.getName());
            }else {
                userCommentDO.setName(userDO.getNickname());
            }
            // 课程
            JazCoursesDO jazCoursesDO = jazCoursesService.selectById(jazCoursesOrderDO.getCoursesId());
            userCommentDO.setCommentObject(jazCoursesDO.getName());
            userCommentDO.setUserImg(userDO.getHeadPortrait());
        }
        // 评价描述 多条
        String[] split = jazUserCommentDO.getCommentDescriptionId().split(",");
        Wrapper<JazUserCommentDescriptionDO> wrapper = new EntityWrapper<JazUserCommentDescriptionDO>().in("id", split);
        List<JazUserCommentDescriptionDO> userCommentDescriptionDOList = jazUserCommentDescriptionService.selectList(wrapper);
        List<String> stringArrayList = new ArrayList<>();
        Long commentClassifyId=userCommentDescriptionDOList.get(0).getCommentClassifyId();
        userCommentDescriptionDOList.stream().forEach(a->{
            stringArrayList.add(a.getCommentDescription());
        });
        userCommentDO.setCommentDescriptionContext(stringArrayList);
        // 评价类型
        JazUserCommentClassifyDO jazUserCommentClassifyDO = jazUserCommentClassifyService.selectById(commentClassifyId);
        userCommentDO.setCommentTypeName(jazUserCommentClassifyDO.getCommentTypeName());
        list.add(userCommentDO);
    }
}
